package cn.school.demo

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.findNavController
import cn.school.demo.databinding.ActivityMainBinding
import cn.school.demo.util.SensitivewordFilter
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.lzy.imagepicker.ImagePicker
import com.lzy.imagepicker.bean.ImageItem
import com.tencent.android.tpush.*
import java.util.concurrent.ExecutionException


class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private lateinit var mMainViewModel: MainViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MyApplication.systemBarSetting(MyApplication.LIGHT, this)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        mMainViewModel = ViewModelProvider(this).get(MainViewModel::class.java)
        this.apply {
            setContentView(binding.root)
            findNavController(R.id.main_activity)
        }

        // onCreate 内获取

        // 当使用 intent uri （客户端自定义）方式推送，且在 intent 尾部添加了参数，可以通过此段代码获取各参数值。
        // 例如推送的 intent 值为 xgscheme://com.tpns.push/notify_detail?param1=aa&param2=bb,
        // 则可以通过此段代码获取到 param1 的值 aa 和 param2 的值 bb。
        //val uri = getIntent().getData();
        //LogUtils.e("onCreate get data uri: " + uri)

        //腾讯TPNS服务配置
        XGPushConfig.enableDebug(this, true)
        XGPushConfig.enableShowInMsg(this,true)
        XGPushManager.registerPush(this, object : XGIOperateCallback {
            override fun onSuccess(data: Any, flag: Int) {
                //token在设备卸载重装的时候有可能会变
                Log.d("TPush", "注册成功，设备token为：$data")
            }

            override fun onFail(data: Any, errCode: Int, msg: String) {
                Log.d("TPush", "注册失败，错误码：$errCode,错误信息：$msg")
            }
        })

        val open = assets.open("sensitiveword.txt")
        LogUtils.e(open)
        val filter = SensitivewordFilter(open)
        MyApplication.filter = filter
        /*val string = ("太多的伤感情怀也许只局限于饲养基地 荧幕中的情节，主人公尝试着去用某种方式渐渐的很潇洒地释自杀指南怀那些自己经历的伤感。"
                + "然后法轮功 我们的扮演的角色就是跟随着主人公的喜红客联盟 怒哀乐而过于牵强的把自己的情感也附加于银幕情节中，然后感动就流泪，"
                + "难过就躺在某一个人的怀里尽情的阐述心扉或者手机卡复制器一个人一杯红酒一部电影在夜三级片 深人静的晚上，关上电话静静的发呆着。许运鸿手淫zhuanfalu")
        val set = filter.getSensitiveWord(string, 1)
        LogUtils.e(
            "敏感词的数量：" + filter.sensitiveWordMap.size,
            "待检测语句字数：" + string.length,
            "语句中包含敏感词的个数为：" + set.size + "。包含：" + set
        )*/

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (resultCode) {
            ImagePicker.RESULT_CODE_ITEMS -> {
                LogUtils.e("我完成图片了", requestCode)
                if (data != null) {
                    val images =
                        data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS) as MutableList<ImageItem>
                    LogUtils.e(images[0].path)

                    MyApplication.apply {
                        setCircleGlideByUrlString(
                            applicationContext,
                            images[0].path,
                            findViewById(R.id.edit_fragment_avatar_image)
                        )
                        imageUrl = images[0].path
                        imageIsModify = true
                        //getBitmap(images[0].path)
                        //bitmap = ImageUtils.getBitmap(images[0].path)
                    }
                    //DataRepository().formUpdate(MyApplication.user!!.sfNumber)
                } else {
                    ToastUtils.showShort("无数据！")
                }
            }
            else -> LogUtils.d("无图片")
        }
    }

    fun getBitmap(url: String) {
        Thread {
            try {
                val bitmap = Glide.with(this@MainActivity).asBitmap() //必须
                    .load(url)
                    .centerCrop()
                    .submit()
                    .get()
                LogUtils.e(bitmap)
            } catch (e: ExecutionException) {
                e.printStackTrace()
            } catch (e: InterruptedException) {
                e.printStackTrace()
            }
        }.start()
    }
}